<?php

include 'connection.php';

function get//NAMECLASS_PLURALIZE//($from, $to) {
	$sql = "//query_select// LIMIT :to OFFSET :from";
	try {
		$db = getConnection();
		$stmt = $db->prepare($sql); 
        $_from = intval($from, 10);
        $_to = intval($to, 10);
		$stmt->bindParam(":from", $_from, PDO::PARAM_INT);
        $stmt->bindParam(":to", $_to, PDO::PARAM_INT);
		$stmt->execute();
        $posts = array();
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
				//query_select_param_references_array//
			    //query_select_param_model_array//
                $posts[] = array('//nameclass//' => $//nameclass// //query_select_param_references_post//);
        }
		$statement = $db->prepare('SELECT count(*) FROM categories');
		$statement->execute();
		$count = $statement->fetchColumn();
	
		$db = null;
		echo '{"result": ' . json_encode($posts) . ',"rows":"'.$count.'"}';
	} catch(PDOException $e) {
		echo '{"error":{"text":'. $e->getMessage() .'}}'; 
	}
}

function get//NAMECLASS_SINGULARIZE//($id) {
	$sql = "SELECT * FROM //nameclass// WHERE id=:id";
	try {
		$db = getConnection();
		$stmt = $db->prepare($sql);  
		$stmt->bindParam("id", $id);
		$stmt->execute();
		$//nameclass// = $stmt->fetchObject();  
		$db = null;
		echo json_encode($//nameclass//); 
	} catch(PDOException $e) {
		echo '{"error":{"text":'. $e->getMessage() .'}}'; 
	}
}

function add//NAMECLASS_SINGULARIZE//() {
	error_log('addWine\n', 3, dirname(__FILE__).'/php.log');
	$request = Slim::getInstance()->request();
	$//nameclass// = json_decode($request->getBody());
	
	$sql = "INSERT INTO //nameclass//(//list_atrrs_create//) values(//list_atrrs_create_values//)";
    try {
		$db = getConnection();
		$stmt = $db->prepare($sql);  
//list_atrrs_create_bind//
		$stmt->execute();
		$//nameclass//->id = $db->lastInsertId();
		$db = null;
		echo json_encode($//nameclass//); 
	} catch(PDOException $e) {
		//error_log($e->getMessage(), 3, '/var/tmp/php.log');
		echo '{"error":{"text":'. $e->getMessage() .'}}'; 
	}
}

function update//NAMECLASS_SINGULARIZE//($id) {
	$request = Slim::getInstance()->request();
	$body = $request->getBody();
	$//nameclass// = json_decode($body);
	
	$sql = "UPDATE //nameclass// SET //list_atrrs_update// WHERE id = :id ";
    try {
		$db = getConnection();
		$stmt = $db->prepare($sql);  
		//list_atrrs_update_bind//
		$stmt->bindParam("id", $id);
		$stmt->execute();
		$db = null;
		echo json_encode($//nameclass//); 
	} catch(PDOException $e) {
		echo '{"error":{"text":'. $e->getMessage() .'}}'; 
	}
}

function delete//NAMECLASS_SINGULARIZE//($id) {
	$sql = "DELETE FROM //nameclass// WHERE id=:id";
	try {
		$db = getConnection();
		$stmt = $db->prepare($sql);  
		$stmt->bindParam("id", $id);
		$stmt->execute();
		$db = null;
	} catch(PDOException $e) {
		echo '{"error":{"text":'. $e->getMessage() .'}}'; 
	}
}

?>
